package com.chzc.satoken;

import cn.dev33.satoken.stp.StpInterface;
import com.chzc.po.Role;
import com.chzc.sys.service.IMenuService;
import com.chzc.sys.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cv大魔王
 * @version 1.0
 * @date 2021/5/24 15:16
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginKey) {
        // 根据id查询用户角色信息
        List<Role> roles = roleService.findRoleListByUserId((String) loginId);
        ArrayList<String> codeList = new ArrayList<>();
        for (Role role : roles) {
            List<String> list = menuService.findMenuCodeByRoleId(role.getId());
            codeList.addAll(list);
        }
        return codeList;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginKey) {
        return roleService.findRoleCodeListByUserId((String) loginId);
    }

}
